
package model;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import org.jdom.CDATA;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import parser.MascaraFactory;
import parser.Parser;

public class NodeFactory
{

   public static Element createChave(String valor)
   {
      Element element = new Element("chave");
      element.setAttribute("nome", valor);
      return element;
   }

   public static Element createParametro(String valor)
   {
      Element element = new Element("parametro");
      processaParametro(element, valor);
      return element;
   }

   public static Element createChaves(String[] values)
   {
      Element[] elements = new Element[values.length];
      for (int i = 0; i < elements.length; i++)
      {
         elements[i] = createChave(values[i]);
      }

      Element chaves = new Element("chaves");
      chaves.addContent(Arrays.asList(elements));
      return chaves;
   }

   public static Element createParametros(String[] params)
   {
      Element[] elements = new Element[params.length];
      for (int i = 0; i < elements.length; i++)
      {
         String param = params[i];
         elements[i] = new Element("parametro");
         processaParametro(elements[i], param);
      }

      Element parametros = new Element("parametros");
      parametros.addContent(Arrays.asList(elements));

      return parametros;
   }

   public static Element createRegra(String tabela, String acao, String[] values, String[] params)
   {

      Element mascara =
          MascaraFactory.createSaida(Tec.EWSD, acao, tabela, values, params);
      Element mascaraRollback =
          MascaraFactory.createRollback(Tec.EWSD, acao, tabela, values, params);

      Element parametros = createParametros(params);
      parametros.addContent(mascara);
      parametros.addContent(mascaraRollback);

      Element regra = new Element("regra");
      Element configuracao = createConfiguracao(parametros);

      Element chaves = createChaves(values);
      regra.addContent(chaves);
      regra.addContent(configuracao);

      return regra;
   }
   
   public static Element createRegra(String tabela, String acao, String[] values, List<String> paramsList) 
   {
      Element regra = new Element("regra");
      Element configuracao = new Element("configuracao");
      Element[] parametros = new Element[paramsList.size()];
      
      for (int i= 0; i < paramsList.size(); i++)
      {
         String[] params = paramsList.get(i).split(",");
         
         Element mascara =
            MascaraFactory.createSaida(Tec.EWSD, acao, tabela, values, params);
         Element mascaraRollback =
            MascaraFactory.createRollback(Tec.EWSD, acao, tabela, values, params);
         
         parametros[i] = createParametros(params);
         parametros[i].addContent(mascara);
         parametros[i].addContent(mascaraRollback);
         
         configuracao.addContent(parametros[i]);
      }      
      
      Element chaves = createChaves(values);
      regra.addContent(chaves);
      regra.addContent(configuracao);
      
      return regra;
   }

   public static Element createConfiguracao(Element parametros)
   {

      Element configuracao = new Element("configuracao");
      configuracao.addContent(parametros);

      return configuracao;
   }

   private static void processaParametro(Element element, String param)
   {
      element.setAttributes(Parser.getAttributes(param));

      if (param.contains("("))
      {
         Element[] valores = processaValores(param.substring(param.indexOf("(") + 1, param.length() - 1).split(";"));
         element.addContent(Arrays.asList(valores));
      }

   }

   private static Element[] processaValores(String[] valores)
   {
      Element[] elements = new Element[valores.length];

      for (int i = 0; i < valores.length; i++)
      {
         String valor = valores[i];
         elements[i] = new Element("valor");

         if (valor.contains("["))
         {
            elements[i].addContent(new CDATA(valor));
         }
         else
         {
            elements[i].setText(valor);
         }
      }

      return elements;
   }

   public static Element createParametroEstatico(String param)
   {
      Element element = new Element("estatico");
      element.setText(param);
      return element;
   }

   public static Element createParametrosEstaticos(String[] params)
   {
      Element[] elements = new Element[params.length];
      for (int i = 0; i < elements.length; i++)
      {
         elements[i] = createParametroEstatico(params[i]);
      }

      Element parametros = new Element("parametrosEstaticos");
      parametros.addContent(Arrays.asList(elements));

      return parametros;
   }

   private static void debug(Element root)
   {

      XMLOutputter xout = new XMLOutputter();
      try
      {
         xout.output(root, System.out);
      }
      catch (IOException ex)
      {
         ex.printStackTrace();
      }

   }

   public static void main(String[] args)
   {
      String[] ch = "vilas,boas".split(",");
      String pa0 = "+asta,*ddd([1-500];510;[521-999];1001),jhh(EE;RR;TT;YY)";
      String pa1 = "opa,*tr([1-500];510;[521-999];1001),apt(YY)";
      String pa2 = "*nois([1-500];510;[521-999];[9999-100001])";

      Vector<String> configs = new Vector<String>();
      configs.add(pa0);
      configs.add(pa1);
      configs.add(pa2);
      
      
      Element regra = createRegra("teste", "entr", ch, configs);

      debug(regra);
      
      System.out.println();
     
      //System.out.println(Arrays.toString(NodeUtil.getChaves(regra)));

   }

}
